package com.elitel.frame.base.component;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
//import com.elitel.common.redis.RedisDbIndex;
//import com.elitel.common.redis.service.RedisCacheService;
import com.elitel.frame.base.service.DBServerService;
import com.elitel.frame.main.entity.BaseSqlconfig;
import com.elitel.frame.main.entity.BaseTreeconfig;
import com.elitel.frame.main.entity.ext.DBServer;
import com.elitel.frame.main.entity.ext.QueryFiled;
import com.elitel.frame.main.entity.ext.ReturnData;
import com.elitel.frame.main.entity.vo.PageResponseVo;

@Component("redisCatchCensus")
public class RedisCatchCensus {

    @Resource
    private RedisCacheComponent redisCacheComponent;
    //    @Resource
//    private RedisCacheService redisCacheService;
    @Resource
    private DBServerService dbServerServiceImpl;
    @Resource
    private DBServerService dBServerServiceImpl;


    //所有字段
    @Cacheable(cacheNames = "DEFAULT", key = "'BASE_SQLCONFIG_'+#guid")
    public List<ReturnData> getSqlConfigCache(String guid) {
//		int rdi = RedisDbIndex.value;
//		String dbKey = "BASE_SQLCONFIG_"+guid;
//		List<ReturnData> list = new ArrayList<>();
//		if(redisCacheService.stringExists(dbKey, rdi)){
//			String cfgStr = redisCacheService.getObject(dbKey, rdi);
//			list = (List<ReturnData>)JSON.parseArray(cfgStr,ReturnData.class);
//		}else{
//			List<BaseSqlconfig> lists = dbServerServiceImpl.selectServerColumn(guid);
//			for(BaseSqlconfig b:lists){
//					ReturnData returnData = new ReturnData();
//					returnData.setShowtype(b.getShowtype());
//					returnData.setFieldtype(b.getRemark());
//					returnData.setFieldname(b.getFiledname());
//					returnData.setFieldnamecn(b.getFieldcaption());
//					list.add(returnData);
//			}
//			String extendStr = JSON.toJSONString(list);
//			redisCacheService.saveObject(dbKey, extendStr, rdi);
//		}

        List<ReturnData> list = new ArrayList<>();
        List<BaseSqlconfig> lists = dbServerServiceImpl.selectServerColumn(guid);
        for (BaseSqlconfig b : lists) {
            ReturnData returnData = new ReturnData();
            returnData.setShowtype(b.getShowtype());
            returnData.setFieldtype(b.getRemark());
            returnData.setFieldname(b.getFiledname());
            returnData.setFieldnamecn(b.getFieldcaption());
            list.add(returnData);
        }
        return list;
    }

    //查询字段
    @Cacheable(cacheNames = "DEFAULT", key = "'BASE_SQLCONFIGQUERY_'+#guid")
    public List<QueryFiled> getSqlConfigCacheQuery(String guid) {
//        int rdi = RedisDbIndex.value;
//        String dbKey = "BASE_SQLCONFIGQUERY_" + guid;
//        List<QueryFiled> list = new ArrayList<>();
//        if (redisCacheService.stringExists(dbKey, rdi)) {
//            String cfgStr = redisCacheService.getObject(dbKey, rdi);
//            list = (List<QueryFiled>) JSON.parseArray(cfgStr, QueryFiled.class);
//        } else {
//            list = dbServerServiceImpl.selectServerField(guid);
//            String extendStr = JSON.toJSONString(list);
//            redisCacheService.saveObject(dbKey, extendStr, rdi);
//        }

        List<QueryFiled> list = dbServerServiceImpl.selectServerField(guid);
        return list;
    }

    //添加修改删除更改缓存

    //    public void setExtAllSql(String guid) {
//        int rdi = RedisDbIndex.value;
//        String dbKey1 = "BASE_SQLCONFIGQUERY_" + guid;
//        String dbKey2 = "BASE_SQLCONFIG_" + guid;
//        List<ReturnData> list1 = new ArrayList<>();
//        List<ReturnData> list2 = new ArrayList<>();
//        List<BaseSqlconfig> lists = dbServerServiceImpl.selectServerColumns(guid);
//        for (BaseSqlconfig b : lists) {
////			if (b.getIsqueryfield()==1) {
////				ReturnData returnData = new ReturnData();
////				returnData.setShowtype(b.getShowtype());
////				returnData.setFieldtype(b.getRemark());
////				returnData.setFieldname(b.getFiledname());
////				returnData.setFieldnamecn(b.getFieldcaption());
////				list1.add(returnData);
////			}
//            ReturnData returnData = new ReturnData();
//            returnData.setShowtype(b.getShowtype());
//            returnData.setFieldtype(b.getRemark());
//            returnData.setFieldname(b.getFiledname());
//            returnData.setFieldnamecn(b.getFieldcaption());
//            list2.add(returnData);
//        }
//        String extendStr1 = JSON.toJSONString(list1);
//        String extendStr2 = JSON.toJSONString(list2);
//        redisCacheService.saveObject(dbKey1, extendStr1, rdi);
//        redisCacheService.saveObject(dbKey2, extendStr2, rdi);
//    }
    @CachePut(value = CacheConfig.DEFAULT, key = "'BASE_SQLCONFIG_'+#guid")
    public List<ReturnData> setExtAllSql(String guid) {
        List<ReturnData> list2 = new ArrayList<>();
        List<BaseSqlconfig> lists = dbServerServiceImpl.selectServerColumns(guid);
        for (BaseSqlconfig b : lists) {
            ReturnData returnData = new ReturnData();
            returnData.setShowtype(b.getShowtype());
            returnData.setFieldtype(b.getRemark());
            returnData.setFieldname(b.getFiledname());
            returnData.setFieldnamecn(b.getFieldcaption());
            list2.add(returnData);
        }
        return list2;
    }

    //查询字段添加修改删除刷新缓存
    public void addFileCatch() {
    }
}
